package com.mes.controller.warehouse;

import com.mes.entity.User;
import com.mes.service.warehouse.SxkSjListService;
import com.yy.base.page.Message;
import com.yy.base.util.UserUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("sxkSjList")
public class SxkSjListController {
    @Autowired
    private SxkSjListService sxkSjListService;
    //查询所有领料单
    @ResponseBody
    @RequestMapping(value="selectAllList",method= RequestMethod.POST)
    public Message selectAllList(HttpServletRequest request){
        Message msg = new Message();

        // msg.setData("AllLists", sxkSjListService.selectSxListByCode());

        msg.setData("AllLists", sxkSjListService.selectEquipmentByCode());
        msg.setData("ColorLists", sxkSjListService.selectColor());
        msg.setData("ProductLists", sxkSjListService.selectProductByCode());

        return msg;

    }

    //根据领料单号查询所带数据
    @ResponseBody
    @RequestMapping(value="selectPlanLists",method= RequestMethod.POST)
    public Message selectPlanLists(HttpServletRequest request){
        Message msg = new Message();

       /* String code = request.getParameter("llCode").toString();   //领料单号

        msg.setData("PlanLists", sxkSjListService.selectSxkPlanListByCode(code));*/

        String eqId = request.getParameter("llCode").toString();   //设备ID

        msg.setData("PlanLists", sxkSjListService.selectSxkEquipment(eqId));

        return msg;

    }

    //查询颜色
    @ResponseBody
    @RequestMapping(value="selectColorLists",method= RequestMethod.POST)
    public Message selectColorLists(HttpServletRequest request){
        Message msg = new Message();


        msg.setData("ColorLists", sxkSjListService.selectColor());

        return msg;

    }

    //保存上架信息
    @ResponseBody
    @RequestMapping(value="savePlanLists",method= RequestMethod.POST)
    public Message savePlanLists(HttpServletRequest request){
        Message msg = new Message();
        try {

            String Eqid = request.getParameter("EQId").toString();   //设备ID
            String colorID = request.getParameter("COLORID").toString();   //颜色ID
            String wlID = request.getParameter("PRODUCTID").toString();   //物料ID
            String qty = request.getParameter("qtys").toString();   //数量
            String batchID = "";  //库存批次ID
            String opt_mode = "纱线库上架"; //出入库方式
            String status = "OK"; //状态
            double qtys = Double.valueOf(qty.toString());

            User user = UserUtil.getLoginUser(request);

            String adorgId = user.getOrgId();         //登录人的ad_org_id
            String adclientId = user.getClientId();   //登录人的ad_client_id
            String userIdtime = user.getId();			//登录人的ad_user_id
            SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            Date time  =sdf.parse(sdf.format(new Date())); //获取当前系统时间

            List<Map<String, Object>> ListUser = sxkSjListService.getEquipmentputCreateby(userIdtime);

            String createBy = ListUser.get(0).get("CREATEDBY").toString();  //登录人的CreateBy

            String updatedBy = ListUser.get(0).get("UPDATEDBY").toString(); //登录人的updateBy

            List<Map<String, Object>> storage  = sxkSjListService.selectStorageBatch(wlID,colorID);

            if(storage == null || storage.size() == 0){
                msg.setData("QTYS","false");
                return msg;
            }
            batchID = storage.get(0).get("BATCHID").toString();
            String  SUMQTYS = storage.get(0).get("SUMQTYS").toString();
            double numbers = Double.valueOf(SUMQTYS.toString());

            if(numbers == 0 || numbers < qtys){
                msg.setData("QTYS","false");
                return msg;
            }

            sxkSjListService.saveStorageLists(batchID,"71622626D896478F85671DECAFD41976",qtys,"AAEE8C6D791D4ED4B0442DAC387E9056",opt_mode,"",Eqid,status,adorgId,adclientId,createBy,updatedBy,time);


           /* String code = request.getParameter("llcodes").toString();   //领料单号
            String batchID = "";  //库存批次ID
            String wlID = "";     //物料ID
            String colorID = "";  //颜色ID
            String HOUSEID = "";  //仓库ID
            String sizeID = "";   //尺码ID
            String llID = "";     //领料单ID（opt_code）
            String llmxID = "";    //领料单明细ID（data_id）
            double qty = 0;      //数量
            String dwID = "";     //单位ID
            String opt_mode = "纱线库上架"; //出入库方式
            String status = "OK"; //状态

            User user = UserUtil.getLoginUser(request);

            String adorgId = user.getOrgId();         //登录人的ad_org_id
            String adclientId = user.getClientId();   //登录人的ad_client_id
            String userIdtime = user.getId();			//登录人的ad_user_id
            SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            Date time  =sdf.parse(sdf.format(new Date())); //获取当前系统时间

            List<Map<String, Object>> ListUser = sxkSjListService.getEquipmentputCreateby(userIdtime);

            String createBy = ListUser.get(0).get("CREATEDBY").toString();  //登录人的CreateBy

            String updatedBy = ListUser.get(0).get("UPDATEDBY").toString(); //登录人的updateBy

            List<Map<String, Object>> ListPlan = sxkSjListService.selectSxkPlanListByCode(code);

            if(ListPlan.size() != 0 ){

                for(int i = 0; i<ListPlan.size() ; i++){
                    wlID =  ListPlan.get(i).get("WLID").toString();
                    if(ListPlan.get(i).get("COLORID") != null && !ListPlan.get(i).get("COLORID").equals("") ){
                        colorID = ListPlan.get(i).get("COLORID").toString();
                    }

                    if(ListPlan.get(i).get("CMID") != null && !ListPlan.get(i).get("CMID").equals("") ){
                        sizeID = ListPlan.get(i).get("CMID").toString();
                    }
                    HOUSEID = ListPlan.get(i).get("CKID").toString();
                    llID = ListPlan.get(i).get("LLID").toString();
                    llmxID = ListPlan.get(i).get("MXID").toString();
                    String a = ListPlan.get(i).get("QTY").toString();
                    qty = Double.parseDouble(a);
                    dwID = ListPlan.get(i).get("DWID").toString();

                    List<Map<String, Object>> storage  = sxkSjListService.selectStorageBatch(wlID,sizeID,colorID);

                    batchID = storage.get(0).get("BATCHID").toString();


                   sxkSjListService.saveStorageLists(batchID,HOUSEID,qty,dwID,opt_mode,llID,llmxID,status,adorgId,adclientId,createBy,updatedBy,time);
                   sxkSjListService.updateStorageMx(llmxID);
                }

            }*/
        }catch (Exception e){
            msg.setData("results","false");
            e.printStackTrace();
        }
        return msg;

    }
}
